<template>
	<om-formsub-outer :index="index" :checkedit="checkedit">
		<om-autoform ref="autoform" :initConfigData="autoformdata" :initValue="initValue"></om-autoform>
	</om-formsub-outer>
</template>

<script>
	import {buildSubFormData, subOnLoad,getEditorTitle,changeEditor,configSelectData,hasValue} from '@/static/js/formUtil.js';
	import { ignoreHtml,ptFloat,minusF,getDateStr} from '@/static/js/common.js';
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return buildSubFormData({
				indexCache:"obj-year-tuserAnnualTargetPlan-index-cache",
				dutyOffCache:'obj-year-tuserAnnualTargetPlan-dutyOff-cache',
				index1Cache:"obj-year-tuserAnnualTargetPlan-index1-cache",
				index2Cache:"obj-year-tuserAnnualTargetPlan-index2-cache",
				 
				target1Cache:'obj-year-tuserAnnualTargetPlan-target1-cache',
				targetCache:'obj-year-tuserAnnualTargetPlan-target-cache'
			})
		},
		methods:{
			onChallengeTargetCode(item){
				let _that = this
				_that.$refs.autoform.formdata.challengeTargetName = _that.$refs.autoform.formdata.logicRelationshipName + item + _that.$refs.autoform.formdata.indexUnitName;
			},
			onTargetValueCode(item){
				let _that = this
				_that.$refs.autoform.formdata.targetValueName = _that.$refs.autoform.formdata.logicRelationshipName + item + _that.$refs.autoform.formdata.indexUnitName;
			},
			checkIndexAttrId(id){
				let _that = this
				if(id == "36583"){
					let arr = _that.datas.filter((e)=>{
						if((e.orderNum != _that.$refs.autoform.formdata.orderNum) && (e.indexAttrCode != "36583")){
							return e
						}
					})
					uni.setStorage({
						key:_that.index2Cache,
						data:arr,
						success:(res)=>{
							_that.$refs.autoform.changeEditor("title_index","indexCode1","type","linkbox")
							let title = _that.$refs.autoform.getEditorTitle("title_index","indexId")
							_that.$refs.autoform.changeEditor("title_index","indexCode1",{
								link:"../../public/search/search",
								"linkParam":{
									other:JSON.stringify({}),
									cachename:_that.index1Cache,
									otherCache:_that.index2Cache,
									defaultData:true,
									initload:true,
									namefield:"indexName",
									extrafield:'orderNum',
									placeholder:"请输入"+title
								}
							})
						}
					})
				}else{
					_that.$refs.autoform.changeEditor("title_index","indexCode1","type","labelbox")
				}
			},
			inputWeight(val){
				let _that = this
				if(Number(val) > 100){
					_that.$refs.autoform.formdata.weight = 100
				}else if(Number(val) < 1){
					_that.$refs.autoform.formdata.weight = 1
				}
			},
			selectChecksId(data){
				let _that = this
				_that.$refs.autoform.changeEditor("title_index", "weight","required",true)
				_that.$refs.autoform.changeEditor("title_index", "weight", "type", "numberbox")
			},
			clearChecksId(){
				let _that = this
				_that.$refs.autoform.clear("title_index","checksCode")
				_that.$refs.autoform.clear("title_index","weight")
				_that.$refs.autoform.changeEditor("title_index", "weight","required",false)
			},
			getCheckList(){
				let _that = this
				_that.om.request(
					"torgAnnualTargetPlan/getCheckList"
				).then(res=>{
					_that.$refs.autoform.changeEditor("title_index","checksCode","type","selectbox")
					_that.$refs.autoform.changeEditor("title_index","checksCode","data",configSelectData(res,"id","text"))
					_that.$refs.autoform.initSelectIndex("title_index","checksCode")
				})
			},
			getIndexOriginList(){
				let _that = this
				_that.om.request(
					"tinstAnnualTargetPlan/getIndexOriginList"
				).then(res=>{
					_that.$refs.autoform.changeEditor("title_index","indexOriginId","type","selectbox")
					_that.$refs.autoform.changeEditor("title_index","indexOriginId","data",configSelectData(res,"id","dicname"))
					_that.$refs.autoform.initSelectIndex("title_index","indexOriginId")
				})
			},
			getIndexUserList(id) {
				let _that = this
				_that.$refs.autoform.changeEditor("title_index", "dutyOffId","required",true)
				_that.$refs.autoform.changeEditor("title_index", "dutyOffId","type","linkbox")
				_that.$refs.autoform.changeEditor("title_index", "dutyOffId", {
					link: "../../public/search/search",
					"linkParam": {
						other: JSON.stringify({
							instId: _that.instId,
							indexId: id
						}),
						url: "orgInstPosUserOfficial/findSelfListByUserIdOrgId",
						namefield: "userPosName",
						extrafield: 'instName',
						initload: true,
						cachename: _that.dutyOffCache
					}
				})
			},
			getCategoryList(id){
				let _that = this
				_that.om.request(
					"indexCategory/findIndexCategoryList",
					{
						indexDictId: id
					}
				).then(res=>{
					_that.$refs.autoform.changeEditor("title_index",'rangeId','type','selectbox')
					_that.$refs.autoform.changeEditor("title_index",'rangeId',"data",configSelectData(res,"indexCategoryId", "indexCategoryName"))
					_that.$refs.autoform.initSelectIndex("title_index",'rangeId')
				})
			},
			changeType(logicRelationshipName,indexUnitName){
				let _that = this
				_that.$refs.autoform.formdata.logicRelationshipName = logicRelationshipName
				_that.$refs.autoform.formdata.indexUnitName =  indexUnitName
				_that.$refs.autoform.changeEditor("title_index", "targetValueCode","required",true)
				_that.$refs.autoform.changeEditor("title_index", "targetValueCode", "type","numberbox")
				_that.$refs.autoform.changeEditor("title_index", "targetValueCode", "prefix", logicRelationshipName)
				_that.$refs.autoform.changeEditor("title_index", "targetValueCode", "suffix", indexUnitName)
				_that.$refs.autoform.changeEditor("title_index", "challengeTargetCode", "type","numberbox")
				_that.$refs.autoform.changeEditor("title_index", "challengeTargetCode", "suffix", indexUnitName)
				_that.$refs.autoform.changeEditor("title_index", "remark_", "type", "textarea")
				_that.$refs.autoform.changeEditor("title_index", "remark_", "prompt", "请输入")
				
			},
		},
		onShow() {
			let _that = this
			if(_that.$refs.autoform){
				uni.getStorage({
					key:_that.indexCache,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.indexId = d.id
							formdata.indexName = d.dicname
							formdata.indexAttrCode = d.indexAttrCode
							formdata.indexAttrName = d.indexAttrName
							formdata.weight = d.power
							_that.changeType(d.logicRelationshipName,d.indexUnitName)
							_that.getCategoryList(d.id)
							_that.getIndexUserList(d.id)
							_that.getIndexOriginList()
							_that.getCheckList()
							let pArr = []
							let rArr = []
							let objFormCode = JSON.parse(uni.getStorageSync('objFormCode')) 
							// if(d.indexAttrCode == 464){
							// 	if(_that.datas.length ==0){
							// 		formdata.orderNum = objFormCode.DICT_CODE_INDEX_P+1
							// 	}else{
							// 		for(let item of _that.datas){
							// 			if(item.orderNum.indexOf(objFormCode.DICT_CODE_INDEX_P) != -1 ){
							// 				pArr.push(item)
							// 				let num = pArr[pArr.length-1].orderNum.match(/\d+/g)
							// 				formdata.orderNum = objFormCode.DICT_CODE_INDEX_P+(Number(num)+1)
							// 			}else{
							// 				formdata.orderNum = objFormCode.DICT_CODE_INDEX_P+1
							// 			}
							// 		}
							// 	}
							// }else{
								if(_that.datas.length ==0){
										formdata.orderNum = objFormCode.DICT_CODE_R+1
									}else{
										for(let item of _that.datas){
											if(item.orderNum.indexOf(objFormCode.DICT_CODE_R)!= -1){
												rArr.push(item)
												let num = rArr[rArr.length-1].orderNum.match(/\d+/g)
												formdata.orderNum = objFormCode.DICT_CODE_R+(Number(num)+1)
											}else{
												formdata.orderNum = objFormCode.DICT_CODE_R+1
											}
										}
									}
								// }
								_that.checkIndexAttrId(d.indexAttrCode)
							uni.removeStorage({
								key:_that.indexCache
							})
						}
					}
				})
				uni.getStorage({
					key:_that.dutyOffCache,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.dutyOffId = d.id
							formdata.dutyOffName = d.userName
							formdata.dutyInstName = d.instName
							uni.removeStorage({
								key:_that.dutyOffCache
							})
						}
					}
				})
				uni.getStorage({
					key:_that.index1Cache,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.indexCode1 = d.orderNum
							uni.removeStorage({
								key:_that.index1Cache
							})
						}
					}
				})
				uni.getStorage({
					key:_that.targetCache,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.targetCode = d.orderNum
							uni.removeStorage({
								key:_that.targetCache
							})
						}
					}
				})
			}
		},
		onLoad(e) {
			let _that = this
			subOnLoad(_that, e, function() {
			if(e.configDataCacheKey){
				_that.targetList = JSON.parse(e.targetList)
				uni.getStorage({
					key: e.configDataCacheKey,
					success: function(res) {
						if(res.data && Object.prototype.toString.call(res.data)==='[object Array]'){
							_that.autoformdata = res.data
							Object.assign(_that.initValue, _that.formdata)
							_that.autoformdata[0].order = _that.order
							let title = getEditorTitle("title_index","indexId",_that.autoformdata)
								changeEditor("title_index","indexId",{
									link:"../../public/search/search",
									"linkParam":{
										other:JSON.stringify({}),
										url:"indexDict/getAllIndex2",
										initload:true,
										namefield: "dicname",
										extrafield: "indexUnitName",
										notefield: "logicRelationshipName",
										cachename:_that.indexCache,
										placeholder:"请输入"+title
									}
								},null,_that.autoformdata)
								uni.setStorage({
									key: _that.target1Cache,
									data:_that.targetList,
									success: (res) => {
										changeEditor("title_index", "targetCode", "type", "linkbox",_that.autoformdata)
										changeEditor("title_index", "targetCode", {
											link: "../../public/search/search",
											"linkParam": {
												other: JSON.stringify({}),
												cachename: _that.targetCache,
												otherCache: _that.target1Cache,
												defaultData: true,
												initload: true,
												namefield: "orderNum",
												placeholder: "请输入" +e.title
											}
										},null,_that.autoformdata)
									}
								})
							}
						}
					})
				}
				_that.$nextTick(function(){
					if(_that.$refs.autoform.formdata.indexId){
						_that.changeType(_that.$refs.autoform.formdata.logicRelationshipName,_that.$refs.autoform.formdata.indexUnitName)
						_that.getCategoryList(_that.$refs.autoform.formdata.indexId)
						_that.getIndexUserList(_that.$refs.autoform.formdata.indexId)
						_that.getIndexOriginList()
						_that.getCheckList()
						_that.checkIndexAttrId(_that.$refs.autoform.formdata.indexAttrCode)
						if(_that.$refs.autoform.formdata.checksCode){
							_that.$refs.autoform.changeEditor("title_index", "weight","required",true)
							_that.$refs.autoform.changeEditor("title_index", "weight", "type", "numberbox")
						}
					}
				})
			})
		}
	}
</script>

<style>
</style>                                                                                                                                                                                                                                                                                                                                                                                                                                                         
